package com.lsy.demo.utils;

import com.alibaba.excel.metadata.Sheet;
import com.alibaba.fastjson.JSON;
import com.lsy.demo.controller.ExcelVO;
import com.lsy.demo.entry.*;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : lsy
 * @version 1.0
 * @description: TODO
 * @date 2021/11/10 18:06
 */

public class LogicUtils {


    /**
     * 工资总表
     * @param excelVO
     * @param filePath
     * @return
     */
    public static Map firstPublic (ExcelVO excelVO,String filePath) {
        Map<String, Object> map = new HashMap<>();
        Sheet sheet = new Sheet(1, 3);
        List<Object> objects = ExcelUtil.readLessThan1000RowBySheet(filePath,sheet);
        List<KOne> listContent = new ArrayList<>();
        List<KOneConditionA> listAddressList = new ArrayList<>();
        List<KOneConditionA> listCondition = new ArrayList<>();
        for (Object obj : objects) {
            ArrayList<String> lineSize = (ArrayList<String>) obj;
            Map<String,String> map1 = new HashMap<>();
            KOneConditionA of = new KOneConditionA();
            for (int i = 0; i <  lineSize.size(); i++) {
                if (Objects.nonNull(lineSize.get(i)) && !StringUtils.isEmpty(lineSize.get(2))) {
                    of = KOneConditionA.of(lineSize.get(2), lineSize.get(4));
                }
                map1.put("K"+(i+1),lineSize.get(i));
            }
            listCondition.add(of);
            KOne user = JSON.parseObject(JSON.toJSONString(map1), KOne.class);
            listContent.add(user);
        }
        List<KOne> listContentResult = new ArrayList<>();
        // 条件1
        if (!StringUtils.isEmpty(excelVO.getAddress())) {
            listContent = listContent.stream().filter(e->e.getK3().equals(excelVO.getAddress())).collect(Collectors.toList());
            // 条件2
            if (!StringUtils.isEmpty(excelVO.getDept())) {
                Map<String, List<KOne>> collect = listContent.stream().collect(Collectors.groupingBy(KOne::getK5));
                listContent.clear();
                String[] split = excelVO.getDept().split(",");
                for (String c : split) {
                    if (!StringUtils.isEmpty(collect.get(c))) {
                        List<KOne> list = collect.get(c);
                        listContent.addAll(list);
                    }
                }
            }
        }
        // 增加分页
        int index = 0;
        for (KOne user : listContent) {
            if (Objects.nonNull(user)) {
                index += 1;
                user.setK0(index);
                listContentResult.add(user);
            }
        }
        // 分页 逻辑在这边
        int begin = (excelVO.getPage() - 1) * excelVO.getPagesize();
        int end = excelVO.getPage() * excelVO.getPagesize();
        listContentResult = listContentResult.stream().filter(e->e.getK0() > begin && e.getK0() <= end).collect(Collectors.toList());
        if (listCondition.size() != 0) {
            Map<String, List<KOneConditionA>> collect = listCondition.stream().collect(Collectors.groupingBy(KOneConditionA::getKey));
            listCondition.clear();
            Set<String> keySet = collect.keySet();
            for (String key : keySet) {
                listAddressList.add(KOneConditionA.of(key,key));
                List<KOneConditionA> vList = collect.get(key);
                Set<String> collect2 = vList.stream().map(KOneConditionA::getValue).collect(Collectors.toSet());
                listCondition.add(KOneConditionA.ofc(key,collect2));
            }
        }
        map.put("listContent",listContentResult);
        map.put("listCondition",listCondition);
        map.put("listAddressList",listAddressList);
        map.put("total",index);
        return map;
    }

    /**
     * 大区—团队管理者
     * @param excelVO
     * @param filePath
     * @return
     */
    public static Map secondPublic (ExcelVO excelVO,String filePath) {
        Map<String, Object> map = new HashMap<>();
        Sheet sheet = new Sheet(2, 3);
        List<Object> objects = ExcelUtil.readLessThan1000RowBySheet(filePath,sheet);
        List<KTwo> listContent = new ArrayList<>();
        List<KOneConditionA> listAddressList = new ArrayList<>();
        List<KOneConditionA> listCondition = new ArrayList<>();
        for (Object obj : objects) {
            ArrayList<String> lineSize = (ArrayList<String>) obj;
            Map<String,String> map1 = new HashMap<>();
            for (int i = 0; i <  lineSize.size(); i++) {
                if (Objects.nonNull(lineSize.get(i)) && !StringUtils.isEmpty(lineSize.get(3))) {
                    KOneConditionA of = KOneConditionA.of(lineSize.get(3), lineSize.get(4));
                    listCondition.add(of);
                }
                map1.put("M"+(i+1),lineSize.get(i));
            }
            KTwo user = JSON.parseObject(JSON.toJSONString(map1), KTwo.class);
            listContent.add(user);
        }
        List<KTwo> listContentResult = new ArrayList<>();
        // 条件1
        if (!StringUtils.isEmpty(excelVO.getAddress())) {
            listContent = listContent.stream().filter(e->e.getM3().equals(excelVO.getAddress())).collect(Collectors.toList());
            // 条件2
            if (!StringUtils.isEmpty(excelVO.getDept())) {
                Map<String, List<KTwo>> collect = listContent.stream().collect(Collectors.groupingBy(KTwo::getM4));
                listContent.clear();
                String[] split = excelVO.getDept().split(",");
                for (String c : split) {
                    if (!StringUtils.isEmpty(collect.get(c))) {
                        List<KTwo> list = collect.get(c);
                        listContent.addAll(list);
                    }
                }
            }
        }
        // 增加分页
        int index = 0;
        for (KTwo user : listContent) {
            if (Objects.nonNull(user)) {
                index += 1;
                user.setM0(index);
                listContentResult.add(user);
            }
        }
        // 分页 逻辑在这边
        int begin = (excelVO.getPage() - 1) * excelVO.getPagesize();
        int end = excelVO.getPage() * excelVO.getPagesize();
        listContentResult = listContentResult.stream().filter(e->e.getM0() > begin && e.getM0() <= end).collect(Collectors.toList());
        if (listCondition.size() != 0) {
            Map<String, List<KOneConditionA>> collect = listCondition.stream().collect(Collectors.groupingBy(KOneConditionA::getKey));
            listCondition.clear();
            Set<String> keySet = collect.keySet();
            for (String key : keySet) {
                listAddressList.add(KOneConditionA.of(key,key));
                List<KOneConditionA> vList = collect.get(key);
                Set<String> collect2 = vList.stream().map(KOneConditionA::getValue).collect(Collectors.toSet());
                listCondition.add(KOneConditionA.ofc(key,collect2));
            }
        }
        map.put("listContent",listContentResult);
        map.put("listCondition",listCondition);
        map.put("listAddressList",listAddressList);
        map.put("total",index);
        return map;
    }


    /**
     * 工作量汇总
     * @param excelVO
     * @param filePath
     * @return
     */
    public static Map thirdPublic (ExcelVO excelVO,String filePath) {
        Map<String, Object> map = new HashMap<>();
        Sheet sheet = new Sheet(3, 2);
        List<Object> objects = ExcelUtil.readLessThan1000RowBySheet(filePath,sheet);
        List<KThree> listContent = new ArrayList<>();
        List<KOneConditionA> listAddressList = new ArrayList<>();
        List<KOneConditionA> listCondition = new ArrayList<>();
        for (Object obj : objects) {
            ArrayList<String> lineSize = (ArrayList<String>) obj;
            Map<String,String> map1 = new HashMap<>();
            for (int i = 0; i <  lineSize.size(); i++) {
                if (Objects.nonNull(lineSize.get(i)) && !StringUtils.isEmpty(lineSize.get(1))) {
                    listCondition.add(KOneConditionA.of(lineSize.get(1),lineSize.get(1)));
                }
                map1.put("R"+(i+1),lineSize.get(i));
            }
            KThree user = JSON.parseObject(JSON.toJSONString(map1), KThree.class);
            listContent.add(user);
        }
        List<KThree> listContentResult = new ArrayList<>();
        // 条件1
        if (!StringUtils.isEmpty(excelVO.getAddress())) {
            listContent = listContent.stream().filter(e->e.getR2().equals(excelVO.getAddress())).collect(Collectors.toList());
            // 条件2
            if (!StringUtils.isEmpty(excelVO.getDept())) {
                Map<String, List<KThree>> collect = listContent.stream().collect(Collectors.groupingBy(KThree::getR4));
                listContent.clear();
                String[] split = excelVO.getDept().split(",");
                for (String c : split) {
                    if (!StringUtils.isEmpty(collect.get(c))) {
                        List<KThree> list = collect.get(c);
                        listContent.addAll(list);
                    }
                }
            }
        }
        // 增加分页
        int index = 0;
        for (KThree user : listContent) {
            if (Objects.nonNull(user)) {
                index += 1;
                user.setR0(index);
                listContentResult.add(user);
            }
        }
        // 分页 逻辑在这边
        int begin = (excelVO.getPage() - 1) * excelVO.getPagesize();
        int end = excelVO.getPage() * excelVO.getPagesize();
        listContentResult = listContentResult.stream().filter(e->e.getR0() > begin && e.getR0() <= end).collect(Collectors.toList());
        if (listCondition.size() != 0) {
            Map<String, List<KOneConditionA>> collect = listCondition.stream().collect(Collectors.groupingBy(KOneConditionA::getKey));
            listCondition.clear();
            Set<String> keySet = collect.keySet();
            for (String key : keySet) {
                listAddressList.add(KOneConditionA.of(key,key));
                List<KOneConditionA> vList = collect.get(key);
                Set<String> collect2 = vList.stream().map(KOneConditionA::getValue).collect(Collectors.toSet());
                listCondition.add(KOneConditionA.ofc(key,collect2));
            }
        }
        map.put("listContent",listContentResult);
        map.put("listCondition",listCondition);
        map.put("listAddressList",listAddressList);
        map.put("total",index);
        return map;
    }



    /**
     * 当月退费—财务提供
     * @param excelVO
     * @param filePath
     * @return
     */
    public static Map fourPublic (ExcelVO excelVO,String filePath) {
        Map<String, Object> map = new HashMap<>();
        Sheet sheet = new Sheet(4, 2);
        List<Object> objects = ExcelUtil.readLessThan1000RowBySheet(filePath,sheet);
        List<KFour> listContent = new ArrayList<>();
        List<KOneConditionA> listAddressList = new ArrayList<>();
        List<KOneConditionA> listCondition = new ArrayList<>();
        for (Object obj : objects) {
            ArrayList<String> lineSize = (ArrayList<String>) obj;
            Map<String,String> map1 = new HashMap<>();
            for (int i = 0; i <  lineSize.size(); i++) {
                if (Objects.nonNull(lineSize.get(i)) && !StringUtils.isEmpty(lineSize.get(1))) {
                    listCondition.add(KOneConditionA.of(lineSize.get(1),lineSize.get(1)));
                }
                map1.put("P"+(i+1),lineSize.get(i));
            }
            KFour user = JSON.parseObject(JSON.toJSONString(map1), KFour.class);
            listContent.add(user);
        }
        List<KFour> listContentResult = new ArrayList<>();
        // 条件1
        if (!StringUtils.isEmpty(excelVO.getAddress())) {
            listContent = listContent.stream().filter(e->e.getP2().equals(excelVO.getAddress())).collect(Collectors.toList());
            // 条件2
            if (!StringUtils.isEmpty(excelVO.getDept())) {
                Map<String, List<KFour>> collect = listContent.stream().collect(Collectors.groupingBy(KFour::getP4));
                listContent.clear();
                String[] split = excelVO.getDept().split(",");
                for (String c : split) {
                    if (!StringUtils.isEmpty(collect.get(c))) {
                        List<KFour> list = collect.get(c);
                        listContent.addAll(list);
                    }
                }
            }
        }
        // 增加分页
        int index = 0;
        for (KFour user : listContent) {
            if (Objects.nonNull(user)) {
                index += 1;
                user.setP0(index);
                listContentResult.add(user);
            }
        }
        // 分页 逻辑在这边
        int begin = (excelVO.getPage() - 1) * excelVO.getPagesize();
        int end = excelVO.getPage() * excelVO.getPagesize();
        listContentResult = listContentResult.stream().filter(e->e.getP0() > begin && e.getP0() <= end).collect(Collectors.toList());
        if (listCondition.size() != 0) {
            Map<String, List<KOneConditionA>> collect = listCondition.stream().collect(Collectors.groupingBy(KOneConditionA::getKey));
            listCondition.clear();
            Set<String> keySet = collect.keySet();
            for (String key : keySet) {
                listAddressList.add(KOneConditionA.of(key,key));
                List<KOneConditionA> vList = collect.get(key);
                Set<String> collect2 = vList.stream().map(KOneConditionA::getValue).collect(Collectors.toSet());
                listCondition.add(KOneConditionA.ofc(key,collect2));
            }
        }
        map.put("listContent",listContentResult);
        map.put("listCondition",listCondition);
        map.put("listAddressList",listAddressList);
        map.put("total",index);
        return map;
    }








}
